package com.generate;

import com.generate.utils.FileUtils;
import org.mybatis.generator.api.ShellRunner;
import result.pojo.TLecturerProp;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author: 许湛司
 * @Date: 2019/8/23 9:21
 */
public class MyBatisGenerator {
    private static void resetPlace(String propAddr){
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = MyBatisGenerator.class.getClassLoader().getResourceAsStream(propAddr);
        // 使用properties对象加载输入流
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取key对应的value值
        String project=properties.getProperty("target.project");
        String resource=properties.getProperty("target.resources");
        String entityPath=project+"/"+properties.getProperty("model.target.package").replace(".","/");
        String mapperPath=project+"/"+properties.getProperty("dao.target.package").replace(".","/");
        String xmlPath=resource+"/"+properties.getProperty("mapper.target.package").replace(".","/");
        FileUtils.deleteAnyone(entityPath);
        FileUtils.deleteAnyone(mapperPath);
        FileUtils.deleteAnyone(xmlPath);
    }

    //根据配置文件,生成相应的model实体类,mapper接口,mapperXMl文件
    public static void main(String[] args) {
        resetPlace("mybatisGeneratorInit.properties");
        args = new String[]{"-configfile", "src\\main\\resources\\generatorConfig.xml", "-overwrite"};
        ShellRunner.main(args);
    }

}
